using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.Properties;

namespace Workflows.Components
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class GlobalVariableSetterComponent:WorkflowComponentBase
    {
        #region props
        private GlobalVariableSetterInstruction _Instruction;
        private GlobalVariableSetterOutput _Output;

        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction = (GlobalVariableSetterInstruction) value; }
        }

        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output = (GlobalVariableSetterOutput) value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public List<FieldSetter> Setters
        {
            get { return this._Instruction.Setters; }
            set { this._Instruction.Setters = value; }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public GlobalVariableSetterComponent()
            :base()
        {
            this.ComponentName = "Global variable setters";
            this.Description = "Set values of global variables of this workflow";
            this.ComponentIcon = Resources.bind;
            this.ComponentImage = Resources.Control;
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.Misc.ToString();

            this._Instruction =
                new GlobalVariableSetterInstruction(
                    this.ComponentID, Guid.Empty, new List<FieldSetter>());
            this._Output = new GlobalVariableSetterOutput(this.ComponentID, Guid.Empty, null);
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            if (this.Setters != null && this.Setters.Count > 0)
            {
                XmlNode setterRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "Setters");
                foreach (FieldSetter fieldSetter in this.Setters)
                {
                    XmlNode setterNode = XmlDataUtil.AddElement(ref xDoc, setterRootNode, "Setter");
                    XmlDataUtil.UpdateAttribute(ref xDoc, setterNode, "GlobalVariableName", fieldSetter.TgtFieldName);
                    XmlDataUtil.UpdateAttribute(ref xDoc, setterNode, "SrcComponentID", fieldSetter.SrcComponentID.ToString());
                    XmlDataUtil.UpdateAttribute(ref xDoc, setterNode, "SrcFieldName", fieldSetter.SrcFieldName);
                    XmlDataUtil.UpdateAttribute(ref xDoc, setterNode, "TransformationCode",
                                                fieldSetter.TransformationCodeSnippet);
                }
            }
            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                GlobalVariableSetterComponent setterComponent = base.Instantiate(xNode) as GlobalVariableSetterComponent;

                setterComponent.Setters = null;
                XmlNodeList setterNodes = xNode.SelectNodes("Setters/Setter");
                if (setterNodes != null && setterNodes.Count > 0)
                {
                    setterComponent.Setters = new List<FieldSetter>();
                    foreach (XmlNode setterNode in setterNodes)
                    {
                        FieldSetter setter = new FieldSetter();
                        setter.TgtFieldName = setterNode.Attributes.GetNamedItem("GlobalVariableName").Value;
                        setter.SrcComponentID = new Guid(setterNode.Attributes.GetNamedItem("SrcComponentID").Value);
                        setter.SrcFieldName = setterNode.Attributes.GetNamedItem("SrcFieldName").Value;
                        setter.TransformationCodeSnippet = setterNode.Attributes.GetNamedItem("TransformationCode").Value;
                        setterComponent.Setters.Add(setter);
                    }
                }

                return setterComponent;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }
    }
}
